//
//  WmcOtherAPI.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/3/29.
//

import Foundation
import RxSwift
import YPNetwork

struct WmcOtherAPI {
    enum AlbumType {
        // 全部相册
        case all
        // 水印相册
        case watermark
    }

    /// 注册设备
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498328&itf=2206290
    /// - Returns: response
    static func registerDevice() -> Observable<YPNetworkResponse> {
        return WmcNetworkRequest.requestData(path: "/api/guest/add") { request in
            request.method = .get
        }
    }

    /// 企业微信二维码接口
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498897&itf=2196580
    /// - Returns: 微信信息model
    static func fetchEnterpriseWechat() -> Observable<WmcWechatModel> {
        return WmcNetworkRequest.requestData(path: "/api/extend/wechat") { request in
            request.method = .get
        }.parseModelWithClass(WmcWechatModel.self)
    }

    /// 分享下载接口
    /// documentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498897&itf=2196559
    /// - Returns: 下载model
    static func fetchShareDownload() -> Observable<WmcShareDownloadModel> {
        return WmcNetworkRequest.requestData(path: "/api/extend/invite") { request in
            request.method = .get
        }.parseModelWithClass(WmcShareDownloadModel.self)
    }

    /// 新增用户反馈
    /// - DucumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498380
    /// - Parameters:
    ///   - phone: 手机号
    ///   - type: 问题类型
    ///   - content: 反馈问题内容
    ///   - imageUrl: 图片   以 , 隔开
    /// - Returns: response
    static func submitFeedback(phone: String, type: String, content: String, imageUrl: [String]? = nil) -> Observable<YPNetworkResponse> {
        var params: [String: Any] = ["phone": phone, "type": type, "note": content]
        if let imageUrl = imageUrl, !imageUrl.isEmpty {
            var urlString: String = ""
            imageUrl.forEach { url in
                if !url.isEmpty {
                    if !urlString.isEmpty {
                        urlString.append(",")
                    }
                    urlString.append("/\(url)")
                }
            }
            params["img"] = urlString
        }
        return WmcNetworkRequest.requestData(path: "/api/feedback/add", parameters: params) { request in
            request.method = .post
        }
    }

    /// 版本更新
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498897&itf=2196574
    /// - Returns: response
    static func fetchVersionInfo() -> Observable<WmcVersionInfoModel> {
        return WmcNetworkRequest.requestData(path: "/api/extend/package?version_name=ioswmc") { request in
            request.method = .get
        }.parseModelWithClass(WmcVersionInfoModel.self)
    }

    /// 是否为用户同步第一张
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=507061&itf=2241922
    /// - Returns: 请求结果
    static func fetchFirstTakePhotoStatus(success: ((YPNetworkResponse) -> Void)? = nil, failure: ((YPNetworkError) -> Void)? = nil) {
        WmcNetworkRequest.requestData(path: "/api/resume/is-first") { request in
            request.method = .get
        } success: { (response: YPNetworkResponse) in
            success?(response)
        } failure: { error in
            failure?(error)
        }
    }

    /// 微信分享相册接口
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498897&itf=2240355
    /// - Parameters:
    ///   - projectId: 项目id
    ///   - albumType: all:全部相册    wmc:水印分类
    ///   - watermarkId: type为wmc时 必传
    /// - Returns: Observable<WmcCloudAlbumShareModel>
    static func fetchShareCloudPhotoAlbum(projectId: String, albumType: AlbumType, watermarkId: String? = nil) -> Observable<WmcCloudAlbumShareModel> {
        let type = albumType == .all ? "all" : "wmc"
        var path = "/api/extend/share?corp_id=\(projectId)&type=\(type)"

        if let watermarkId = watermarkId, !watermarkId.isEmpty {
            path.append("&wm_id=\(watermarkId)")
        }

        return WmcNetworkRequest.requestData(path: path) { request in
            request.method = .get
        }.parseModelWithClass(WmcCloudAlbumShareModel.self)
    }

    /// 分享单个视频接口
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=297881&mod=498897&itf=2287638
    /// - Parameter id: 资源id(对应photo_id)
    /// - Returns: Observable<WmcCloudAlbumShareModel>
    static func fetchShareVideo(id: String) -> Observable<WmcCloudAlbumShareModel> {
        let path = "/api/extend/video-share?resource_id=\(id)"

        return WmcNetworkRequest.requestData(path: path) { request in
            request.method = .get
        }.parseModelWithClass(WmcCloudAlbumShareModel.self)
    }
    
    static func registerPush(token: String, success: ((YPNetworkResponse) -> Void)? = nil, failure: YPNetworkRequestFailure? = nil) {
        WmcNetworkRequest.requestData(path: "/api/guest/add") { request in
            request.method = .get
            request.extraHeaderFields = ["deviceToken": token]
        } success: { (response: YPNetworkResponse) in
            success?(response)
        } failure: { error in
            failure?(error)
        }
    }
}
